package com.njust.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.servlet.http.HttpSession;
import java.util.Locale;

@Controller
public class I18nTestController {


    @Autowired
    private ResourceBundleMessageSource messageSource;

    @RequestMapping("/toLoginPage")
    public String toLoginPage(Locale locale, Model model){
        System.out.println(locale);
        String welcomeinfo = messageSource.getMessage("welcomeinfo", null, locale);
        System.out.println("welcomeinfo = " + welcomeinfo);
        model.addAttribute("method", "toLoginPage");
        return "login.jsp";
    }

    @RequestMapping("/toLoginPage2")
    public String toLoginPage2(@RequestParam(name = "locale", defaultValue = "zh_CN") String localeStr,
                               Locale locale,
                               Model model,
                               HttpSession session){
        Locale l = null;
        System.out.println(localeStr);
        if (localeStr != null && !"".equals(localeStr)){
            l = new Locale(localeStr.split("_")[0], localeStr.split("_")[1]);
        }else {
            l = locale;
        }
        model.addAttribute("method", "toLoginPage2");
        session.setAttribute(SessionLocaleResolver.class.getName() + ".LOCALE", l);
        return "login.jsp";
    }


    @RequestMapping("/toLoginPage3")
    public String toLoginPage3(@RequestParam(name = "locale", defaultValue = "zh_CN") String localeStr,
                               Locale locale,
                               Model model,
                               HttpSession session){
        System.out.println(localeStr);
        model.addAttribute("method", "toLoginPage3");
        return "login.jsp";
    }
}
